package server;

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;

public class Server {
    public Server() {}

    // TODO read from configuration the settings
    // TODO sync mysql rules & iptables 
    
    public static void main(String args[]) {
        Commands comm = null;
        try {
            ConfigurationManager config = 
                new ConfigurationManager(configFilename);
            comm = new Commands(config);
            comm.turnOn();
        } catch (SQLException e)
        {
            System.err.println ("Error message: " + e.getMessage());
            System.err.println ("Error number: " + e.getErrorCode());
            return;
        } catch (Exception e) {
            System.err.println ("Error in configuration!");
            e.printStackTrace();
        	return;
        }
        try {
            FirewallManagerImpl fwManager = new FirewallManagerImpl(comm);
            FirewallManager stub = (FirewallManager) 
            UnicastRemoteObject.exportObject(fwManager, 0);

            // Bind the remote object's stub in the registry
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind("FirewallManager", stub);

            System.err.println("Server ready");
        } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }
    }

    private static final String configFilename = "fw.config";
}
